/*
 * MainPanel.java
 * Copyright (C) 12/30/2005, 18:36 Francisco J. Hernández
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */
package Sindarin;

import NativeRobot.NativeRobot;
import Sindarin.CodeInterpreter.CodeInterpreter;
import Sindarin.Common.Inst;
import Sindarin.ScriptParser.ScriptParser;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;

/**
 *
 * @author  Francisco J. Hernández
 */
public class MainPanel extends javax.swing.JPanel {

    public static final int VERSION = 0;
    public static final int REVISION = 22;
    public static final String TITULO = MainApplet.TITULO;
    public static final String VERSION_STRING = TITULO + ": " + VERSION + "." + REVISION;
    private CodeInterpreter vm;
    private AtrapaTecla tecla;
    private static int contVar = 1;
    //private static final String TIT_VENT_ULTIMA = "Ultima Online";
    private static final String[] TIT_VENT_ULTIMA = {java.util.ResourceBundle.getBundle("Sindarin/config").getString("TITLE_WINDOW1"), java.util.ResourceBundle.getBundle("Sindarin/config").getString("TITLE_WINDOW2")};
    private static final String EXT_SCR = ".sdr";
    private NativeRobot robot;
    private boolean debug = false;  // Vuelca el pseudocodigo del script en la salida de pantalla
    JFileChooser chooser;
    SimpleComponents.SimpleEditorTextArea codeTextArea = new SimpleComponents.SimpleEditorTextArea();
    Sindarin.JTextFieldInteger locXJTextFieldInteger = new Sindarin.JTextFieldInteger();
    Sindarin.JTextFieldInteger locYJTextFieldInteger = new Sindarin.JTextFieldInteger();
    Sindarin.JTextFieldInteger colRJTextFieldInteger = new Sindarin.JTextFieldInteger();
    Sindarin.JTextFieldInteger colGJTextFieldInteger = new Sindarin.JTextFieldInteger();
    Sindarin.JTextFieldInteger colBJTextFieldInteger = new Sindarin.JTextFieldInteger();
    Sindarin.JOutputArea consolaJOutputArea = new Sindarin.JOutputArea(true, true, false);
    private int lineas;
    private MainFrame mainFrame = null;
    private AboutDialog aboutDialog = null;

    /** Creates new form MainPanel */
    public MainPanel() {
        initComponents();

        // Existen graves problemas al a�adir componentes personalizados usando
        // El entorno Netbeans, por lo que me he visto obligado a a�adirlos
        //  directamente en codigo

        // Inicializacion del codeTextArea
        codeTextArea.setColumns(20);
        codeTextArea.setRows(5);
        codeTextArea.addCaretListener(new javax.swing.event.CaretListener() {

            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                codeTextAreaCaretUpdate(evt);
            }
        });

        jPanel5.add(codeTextArea, java.awt.BorderLayout.CENTER);

        //Inicializacion del locXJTextFieldInteger
        locXJTextFieldInteger.setText("JTEXTFIELDINTEGER1");
        java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 4);
        locPanel.add(locXJTextFieldInteger, gridBagConstraints, 4);

        // Inicializaci�n de locYJTextFieldInteger
        locYJTextFieldInteger.setText("JTEXTFIELDINTEGER2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 30;
        locPanel.add(locYJTextFieldInteger, gridBagConstraints, 6);

        // Inicializaci�n del text field del componente R de la pesta�a color
        colRJTextFieldInteger.setText("JTEXTFIELDINTEGER1");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 4);
        colorPanel.add(colRJTextFieldInteger, gridBagConstraints, 4);

        // Inicializaci�n del text field del componente G de la pesta�a color
        colGJTextFieldInteger.setText("JTEXTFIELDINTEGER2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 30;
        colorPanel.add(colGJTextFieldInteger, gridBagConstraints, 6);

        // Inicializaci�n del text field del componente B de la pesta�a color
        colBJTextFieldInteger.setText("JTEXTFIELDINTEGER2");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.ipadx = 30;
        colorPanel.add(colBJTextFieldInteger, gridBagConstraints, 8);

        //Consola de mensajes
        jScrollPane2.setPreferredSize(new java.awt.Dimension(223, 150));
        consolaJOutputArea.setBackground(javax.swing.UIManager.getDefaults().getColor("close_button_border_unsel"));
        consolaJOutputArea.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        consolaJOutputArea.setColumns(20);
        consolaJOutputArea.setLineWrap(true);
        consolaJOutputArea.setRows(5);
        consolaJOutputArea.setText("");
        consolaJOutputArea.setMirror(true);
        consolaJOutputArea.showErr(true);
        consolaJOutputArea.showOut(true);
        jScrollPane2.setViewportView(consolaJOutputArea);

        try {
            robot = new NativeRobot();
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
        tecla = new AtrapaTecla(this, robot, KeyEvent.VK_PAUSE);

        buscaSesionesUO();
        inicDialogos();
        if (!NativeRobot.isNative()) {
            VarTabbedPane.remove(ctrlPanel);
            locCaptButton.setVisible(false);
            locColCaptButton.setVisible(false);
            colCaptButton.setVisible(false);
//            colDsdLocCaptButton.setVisible(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        menuBar = new javax.swing.JMenuBar();
        archivoMenu = new javax.swing.JMenu();
        nuevoMenuItem = new javax.swing.JMenuItem();
        abrirMenuItem = new javax.swing.JMenuItem();
        salvarMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        aboutMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem.setVisible(false);
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator2.setVisible(false);
        salirMenuItem = new javax.swing.JMenuItem();
        ejecMenu = new javax.swing.JMenu();
        runPauseMenuItem = new javax.swing.JMenuItem();
        stopMenuItem = new javax.swing.JMenuItem();
        instMenu = new javax.swing.JMenu();
        callMenuItem = new javax.swing.JMenuItem();
        condIfMenuItem = new javax.swing.JMenuItem();
        whileMenuItem = new javax.swing.JMenuItem();
        repeatMenuItem = new javax.swing.JMenuItem();
        breakMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        clickMenuItem = new javax.swing.JMenuItem();
        dclickMenuItem = new javax.swing.JMenuItem();
        mousemoveMenuItem = new javax.swing.JMenuItem();
        mousepressMenuItem = new javax.swing.JMenuItem();
        mousereleaseMenuItem = new javax.swing.JMenuItem();
        dragMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        sayMenuItem = new javax.swing.JMenuItem();
        kpressMenuItem = new javax.swing.JMenuItem();
        kreleaseMenuItem = new javax.swing.JMenuItem();
        pkeyMenuItem = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        waitMenuItem = new javax.swing.JMenuItem();
        iodelayMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        printMenuItem = new javax.swing.JMenuItem();
        dateMenuItem = new javax.swing.JMenuItem();
        timeMenuItem = new javax.swing.JMenuItem();
        beepMenuItem = new javax.swing.JMenuItem();
        isnativemodeMenuItem = new javax.swing.JMenuItem();
        colorMenuItem = new javax.swing.JMenuItem();
        componentMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        trueMenuItem = new javax.swing.JMenuItem();
        falseMenuItem = new javax.swing.JMenuItem();
        sindarin_versionMenuItem = new javax.swing.JMenuItem();
        sindarin_revisionMenuItem = new javax.swing.JMenuItem();
        editPopupMenu = new javax.swing.JPopupMenu();
        CtrlVarPanel = new javax.swing.JPanel();
        VarTabbedPane = new javax.swing.JTabbedPane();
        ctrlPanel = new javax.swing.JPanel();
        nativeCheckBox = new javax.swing.JCheckBox();
        nombreUoComboBox = new javax.swing.JComboBox();
        findUoButton = new javax.swing.JButton();
        selectOtherWindowButton = new javax.swing.JButton();
        locPanel = new javax.swing.JPanel();
        locOkButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        locNombreMyJTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        locCaptButton = new javax.swing.JButton();
        locColCaptButton = new javax.swing.JButton();
        colorPanel = new javax.swing.JPanel();
        colOkButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        colNombreMyJTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        colCaptButton = new javax.swing.JButton();
        colDsdLocCaptButton = new javax.swing.JButton();
        keyPanel = new javax.swing.JPanel();
        keyOkButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        keyNombreMyJTextField = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        teclaTextField = new javax.swing.JTextField();
        altCheckBox = new javax.swing.JCheckBox();
        ctrlCheckBox = new javax.swing.JCheckBox();
        shiftCheckBox = new javax.swing.JCheckBox();
        numberPanel = new javax.swing.JPanel();
        numOkButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        numTextField = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        numSpinner = new javax.swing.JSpinner();
        stringPanel = new javax.swing.JPanel();
        stringOkButton = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        stringNombreTextField = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        stringValorTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jToolBar2 = new javax.swing.JToolBar();
        saveButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        runPauseButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        jToolBar3 = new javax.swing.JToolBar();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel5 = new javax.swing.JPanel();
        numberList = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        posCursorLabel = new javax.swing.JLabel();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("Sindarin/config"); // NOI18N
        archivoMenu.setText(bundle.getString("FILE")); // NOI18N

        nuevoMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/nuevo.png"))); // NOI18N
        nuevoMenuItem.setText(bundle.getString("NEW")); // NOI18N
        nuevoMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoMenuItemActionPerformed(evt);
            }
        });
        archivoMenu.add(nuevoMenuItem);

        abrirMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/abrir.png"))); // NOI18N
        abrirMenuItem.setText(bundle.getString("OPEN")); // NOI18N
        abrirMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirMenuItemActionPerformed(evt);
            }
        });
        archivoMenu.add(abrirMenuItem);

        salvarMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/salvar.png"))); // NOI18N
        salvarMenuItem.setText(bundle.getString("SAVE")); // NOI18N
        salvarMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarMenuItemActionPerformed(evt);
            }
        });
        archivoMenu.add(salvarMenuItem);
        archivoMenu.add(jSeparator1);

        aboutMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/s.png"))); // NOI18N
        aboutMenuItem.setText(bundle.getString("ABOUT")); // NOI18N
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        archivoMenu.add(aboutMenuItem);
        archivoMenu.add(jSeparator2);

        salirMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/cerrar.png"))); // NOI18N
        salirMenuItem.setText(bundle.getString("EXIT")); // NOI18N
        salirMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirMenuItemActionPerformed(evt);
            }
        });
        archivoMenu.add(salirMenuItem);

        menuBar.add(archivoMenu);

        ejecMenu.setText(bundle.getString("EXECUTE")); // NOI18N

        runPauseMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/run.png"))); // NOI18N
        runPauseMenuItem.setText(bundle.getString("RUN")); // NOI18N
        runPauseMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runPauseMenuItemActionPerformed(evt);
            }
        });
        ejecMenu.add(runPauseMenuItem);

        stopMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/stop.png"))); // NOI18N
        stopMenuItem.setText(bundle.getString("STOP")); // NOI18N
        stopMenuItem.setEnabled(false);
        stopMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopMenuItemActionPerformed(evt);
            }
        });
        ejecMenu.add(stopMenuItem);

        menuBar.add(ejecMenu);

        instMenu.setText(bundle.getString("COMMAND")); // NOI18N

        callMenuItem.setText("call"); // NOI18N
        callMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                callMenuItemActionPerformed(evt);
            }
        });
        instMenu.add(callMenuItem);

        condIfMenuItem.setText("if else");
        condIfMenuItem.setEnabled(false);
        instMenu.add(condIfMenuItem);

        whileMenuItem.setText("while");
        whileMenuItem.setEnabled(false);
        instMenu.add(whileMenuItem);

        repeatMenuItem.setText("repeat");
        repeatMenuItem.setEnabled(false);
        instMenu.add(repeatMenuItem);

        breakMenuItem.setText("break");
        breakMenuItem.setEnabled(false);
        instMenu.add(breakMenuItem);

        exitMenuItem.setText("exit");
        exitMenuItem.setEnabled(false);
        instMenu.add(exitMenuItem);
        instMenu.add(jSeparator3);

        clickMenuItem.setText("click");
        clickMenuItem.setEnabled(false);
        instMenu.add(clickMenuItem);

        dclickMenuItem.setText("dclick");
        dclickMenuItem.setEnabled(false);
        instMenu.add(dclickMenuItem);

        mousemoveMenuItem.setText("mousemove");
        mousemoveMenuItem.setEnabled(false);
        instMenu.add(mousemoveMenuItem);

        mousepressMenuItem.setText("mousepress");
        mousepressMenuItem.setEnabled(false);
        instMenu.add(mousepressMenuItem);

        mousereleaseMenuItem.setText("mouserelease");
        mousereleaseMenuItem.setEnabled(false);
        instMenu.add(mousereleaseMenuItem);

        dragMenuItem.setText("drag");
        dragMenuItem.setEnabled(false);
        instMenu.add(dragMenuItem);
        instMenu.add(jSeparator5);

        sayMenuItem.setText("say");
        sayMenuItem.setEnabled(false);
        instMenu.add(sayMenuItem);

        kpressMenuItem.setText("keypress");
        kpressMenuItem.setEnabled(false);
        instMenu.add(kpressMenuItem);

        kreleaseMenuItem.setText("keyrelease");
        kreleaseMenuItem.setEnabled(false);
        instMenu.add(kreleaseMenuItem);

        pkeyMenuItem.setText("pushkey");
        pkeyMenuItem.setEnabled(false);
        instMenu.add(pkeyMenuItem);
        instMenu.add(jSeparator6);

        waitMenuItem.setText("wait");
        waitMenuItem.setEnabled(false);
        instMenu.add(waitMenuItem);

        iodelayMenuItem.setText("iodelay");
        iodelayMenuItem.setEnabled(false);
        instMenu.add(iodelayMenuItem);
        instMenu.add(jSeparator4);

        printMenuItem.setText("print");
        printMenuItem.setEnabled(false);
        instMenu.add(printMenuItem);

        dateMenuItem.setText("date");
        dateMenuItem.setEnabled(false);
        instMenu.add(dateMenuItem);

        timeMenuItem.setText("time");
        timeMenuItem.setEnabled(false);
        instMenu.add(timeMenuItem);

        beepMenuItem.setText("beep");
        beepMenuItem.setEnabled(false);
        instMenu.add(beepMenuItem);

        isnativemodeMenuItem.setText("isnativemode");
        isnativemodeMenuItem.setEnabled(false);
        instMenu.add(isnativemodeMenuItem);

        colorMenuItem.setText("color");
        colorMenuItem.setEnabled(false);
        instMenu.add(colorMenuItem);

        componentMenuItem.setText("component");
        componentMenuItem.setEnabled(false);
        instMenu.add(componentMenuItem);

        jMenu1.setText(bundle.getString("TEXT_MENU_VAR")); // NOI18N
        jMenu1.setActionCommand("variables de entorno");

        trueMenuItem.setText("true");
        trueMenuItem.setEnabled(false);
        jMenu1.add(trueMenuItem);

        falseMenuItem.setText("false");
        falseMenuItem.setEnabled(false);
        jMenu1.add(falseMenuItem);

        sindarin_versionMenuItem.setText("sindarin_version");
        sindarin_versionMenuItem.setEnabled(false);
        jMenu1.add(sindarin_versionMenuItem);

        sindarin_revisionMenuItem.setText("sindarin_revision");
        sindarin_revisionMenuItem.setEnabled(false);
        jMenu1.add(sindarin_revisionMenuItem);

        instMenu.add(jMenu1);

        menuBar.add(instMenu);

        setPreferredSize(new java.awt.Dimension(425, 550));
        setLayout(new java.awt.BorderLayout());

        CtrlVarPanel.setLayout(new javax.swing.BoxLayout(CtrlVarPanel, javax.swing.BoxLayout.LINE_AXIS));

        nativeCheckBox.setSelected(true);
        nativeCheckBox.setText(bundle.getString("NATIVE_MODE")); // NOI18N
        nativeCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        nativeCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                nativeCheckBoxStateChanged(evt);
            }
        });
        ctrlPanel.add(nativeCheckBox);

        nombreUoComboBox.setEditable(true);
        ctrlPanel.add(nombreUoComboBox);

        findUoButton.setText(bundle.getString("SEARCH_WIN")); // NOI18N
        findUoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findUoButtonActionPerformed(evt);
            }
        });
        ctrlPanel.add(findUoButton);

        selectOtherWindowButton.setText(bundle.getString("TEXT_OTHER_WINDOW")); // NOI18N
        selectOtherWindowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectOtherWindowButtonActionPerformed(evt);
            }
        });
        ctrlPanel.add(selectOtherWindowButton);

        VarTabbedPane.addTab(bundle.getString("CONTROL"), ctrlPanel); // NOI18N

        locPanel.setLayout(new java.awt.GridBagLayout());

        locOkButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/flecha.jpg"))); // NOI18N
        locOkButton.setBorder(null);
        locOkButton.setBorderPainted(false);
        locOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locOkButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        locPanel.add(locOkButton, gridBagConstraints);

        jLabel1.setText(bundle.getString("TEXT_NAME")); // NOI18N
        locPanel.add(jLabel1, new java.awt.GridBagConstraints());

        locNombreMyJTextField.setText("loc1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 43;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 1);
        locPanel.add(locNombreMyJTextField, gridBagConstraints);

        jLabel2.setText("X:"); // NOI18N
        locPanel.add(jLabel2, new java.awt.GridBagConstraints());

        jLabel3.setText("Y:"); // NOI18N
        locPanel.add(jLabel3, new java.awt.GridBagConstraints());

        locCaptButton.setText(bundle.getString("CAPTURE")); // NOI18N
        locCaptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locCaptButtonActionPerformed(evt);
            }
        });
        locPanel.add(locCaptButton, new java.awt.GridBagConstraints());

        locColCaptButton.setText(bundle.getString("CAPTURE_LOC_COLOR")); // NOI18N
        locColCaptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locColCaptButtonActionPerformed(evt);
            }
        });
        locPanel.add(locColCaptButton, new java.awt.GridBagConstraints());

        VarTabbedPane.addTab("Location", locPanel);

        colorPanel.setLayout(new java.awt.GridBagLayout());

        colOkButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/flecha.jpg"))); // NOI18N
        colOkButton.setBorder(null);
        colOkButton.setBorderPainted(false);
        colOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colOkButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        colorPanel.add(colOkButton, gridBagConstraints);

        jLabel6.setText(bundle.getString("TEXT_NAME")); // NOI18N
        colorPanel.add(jLabel6, new java.awt.GridBagConstraints());

        colNombreMyJTextField.setText("col1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 56;
        colorPanel.add(colNombreMyJTextField, gridBagConstraints);

        jLabel4.setForeground(java.awt.Color.red);
        jLabel4.setText("R:"); // NOI18N
        colorPanel.add(jLabel4, new java.awt.GridBagConstraints());

        jLabel5.setForeground(java.awt.Color.green);
        jLabel5.setText("G:"); // NOI18N
        colorPanel.add(jLabel5, new java.awt.GridBagConstraints());

        jLabel7.setForeground(java.awt.Color.blue);
        jLabel7.setText("B:"); // NOI18N
        colorPanel.add(jLabel7, new java.awt.GridBagConstraints());

        colCaptButton.setText(bundle.getString("CAPTURE")); // NOI18N
        colCaptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colCaptButtonActionPerformed(evt);
            }
        });
        colorPanel.add(colCaptButton, new java.awt.GridBagConstraints());

        colDsdLocCaptButton.setLabel(bundle.getString("CAPTURE_LOC")); // NOI18N
        colDsdLocCaptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colDsdLocCaptButtonActionPerformed(evt);
            }
        });
        colorPanel.add(colDsdLocCaptButton, new java.awt.GridBagConstraints());

        VarTabbedPane.addTab("Color", colorPanel);

        keyPanel.setLayout(new java.awt.GridBagLayout());

        keyOkButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/flecha.jpg"))); // NOI18N
        keyOkButton.setBorder(null);
        keyOkButton.setBorderPainted(false);
        keyOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keyOkButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        keyPanel.add(keyOkButton, gridBagConstraints);

        jLabel8.setText(bundle.getString("TEXT_NAME")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 1;
        keyPanel.add(jLabel8, gridBagConstraints);

        keyNombreMyJTextField.setText("key1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 51;
        keyPanel.add(keyNombreMyJTextField, gridBagConstraints);

        jLabel9.setText(bundle.getString("TEXT_KEY")); // NOI18N
        keyPanel.add(jLabel9, new java.awt.GridBagConstraints());

        teclaTextField.setBackground(java.awt.Color.lightGray);
        teclaTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                teclaTextFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                teclaTextFieldKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 40;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 7);
        keyPanel.add(teclaTextField, gridBagConstraints);

        altCheckBox.setText("alt"); // NOI18N
        altCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        keyPanel.add(altCheckBox, new java.awt.GridBagConstraints());

        ctrlCheckBox.setText("ctrl"); // NOI18N
        ctrlCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        keyPanel.add(ctrlCheckBox, new java.awt.GridBagConstraints());

        shiftCheckBox.setText("shift"); // NOI18N
        shiftCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        keyPanel.add(shiftCheckBox, new java.awt.GridBagConstraints());

        VarTabbedPane.addTab("MacroKey", keyPanel);

        numberPanel.setLayout(new java.awt.GridBagLayout());

        numOkButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/flecha.jpg"))); // NOI18N
        numOkButton.setBorder(null);
        numOkButton.setBorderPainted(false);
        numOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numOkButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        numberPanel.add(numOkButton, gridBagConstraints);

        jLabel10.setText(bundle.getString("TEXT_NAME")); // NOI18N
        numberPanel.add(jLabel10, new java.awt.GridBagConstraints());

        numTextField.setText("number1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 9;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        numberPanel.add(numTextField, gridBagConstraints);

        jLabel11.setText(bundle.getString("TEXT_VALUE")); // NOI18N
        numberPanel.add(jLabel11, new java.awt.GridBagConstraints());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 28;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 8);
        numberPanel.add(numSpinner, gridBagConstraints);

        VarTabbedPane.addTab("Number", numberPanel);

        stringPanel.setLayout(new java.awt.GridBagLayout());

        stringOkButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/flecha.jpg"))); // NOI18N
        stringOkButton.setBorder(null);
        stringOkButton.setBorderPainted(false);
        stringOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stringOkButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        stringPanel.add(stringOkButton, gridBagConstraints);

        jLabel12.setText(bundle.getString("TEXT_NAME")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 1);
        stringPanel.add(jLabel12, gridBagConstraints);

        stringNombreTextField.setText("text1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 23;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        stringPanel.add(stringNombreTextField, gridBagConstraints);

        jLabel13.setText(bundle.getString("TEXT_VALUE")); // NOI18N
        stringPanel.add(jLabel13, new java.awt.GridBagConstraints());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 70;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        stringPanel.add(stringValorTextField, gridBagConstraints);

        VarTabbedPane.addTab("String", stringPanel);

        CtrlVarPanel.add(VarTabbedPane);

        add(CtrlVarPanel, java.awt.BorderLayout.NORTH);

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS));

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/salvar.png"))); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(saveButton);

        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/abrir.png"))); // NOI18N
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(openButton);

        jPanel3.add(jToolBar2);

        runPauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/run.png"))); // NOI18N
        runPauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runPauseButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(runPauseButton);

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/stop.png"))); // NOI18N
        stopButton.setEnabled(false);
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(stopButton);

        jPanel3.add(jToolBar1);
        jPanel3.add(jToolBar3);

        add(jPanel3, java.awt.BorderLayout.EAST);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(223, 150));
        add(jScrollPane2, java.awt.BorderLayout.SOUTH);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel5.setLayout(new java.awt.BorderLayout());

        numberList.setBackground(java.awt.Color.lightGray);
        numberList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jPanel5.add(numberList, java.awt.BorderLayout.WEST);

        jScrollPane1.setViewportView(jPanel5);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel4, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout());

        posCursorLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        posCursorLabel.setText("1:1"); // NOI18N
        posCursorLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        posCursorLabel.setFocusable(false);
        posCursorLabel.setPreferredSize(new java.awt.Dimension(60, 21));
        jPanel1.add(posCursorLabel, java.awt.BorderLayout.WEST);

        jPanel2.add(jPanel1, java.awt.BorderLayout.SOUTH);

        add(jPanel2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void codeTextAreaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_codeTextAreaCaretUpdate
        int posAct = 0;
        try {
// TODO add your handling code here:
            posAct = codeTextArea.getLineOfOffset(codeTextArea.getCaretPosition()) + 1;
            //numberList.add((Object)new Integer(lineas));
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
        lineas = Math.max(lineas, posAct);
        posCursorLabel.setText("" + lineas);
    }//GEN-LAST:event_codeTextAreaCaretUpdate

    public void inicDialogos() {
        chooser = new JFileChooser();

        chooser.addChoosableFileFilter(new Filtro("txt", java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_FORMAT_TXT")));
        chooser.addChoosableFileFilter(new Filtro(EXT_SCR.substring(1), java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_FORMAT_SCR")));

        chooser.setMultiSelectionEnabled(false);
    }

    private void findUoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findUoButtonActionPerformed
// TODO add your handling code here:
        buscaSesionesUO();
    }//GEN-LAST:event_findUoButtonActionPerformed

    private void buscaSesionesUO() {
        if (NativeRobot.isNative()) {

            boolean encontrado = false;
            for (int i = 0; i < TIT_VENT_ULTIMA.length; i++) {
                if (robot.findAllTittleWindow(TIT_VENT_ULTIMA[i], true)) {
                    HashMap tmp = NativeRobot.getVentNativas();
                    Object[] tmpString = tmp.keySet().toArray();
                    nombreUoComboBox.setModel(new javax.swing.DefaultComboBoxModel(tmpString));
                    nombreUoComboBox.setSelectedItem(NativeRobot.getActWinTittle());
                    nombreUoComboBox.setEnabled(true);
                    nativeCheckBox.setEnabled(true);
                    NativeRobot.setNativeMode(nativeCheckBox.isSelected());
                    System.out.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_WINDOW") + NativeRobot.getActWinTittle() + java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_FIND"));
                    encontrado = true;
                }
            }

            if (!encontrado) {
                nombreUoComboBox.setEnabled(false);
                nativeCheckBox.setEnabled(false);
                NativeRobot.setNativeMode(false);
                System.err.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("WARNING_NO_FOUND_WINDOW"));
            }
        }

    }

    private void nativeCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_nativeCheckBoxStateChanged
// TODO add your handling code here:
        NativeRobot.setNativeMode(nativeCheckBox.isSelected());
    }//GEN-LAST:event_nativeCheckBoxStateChanged

    private void stringOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stringOkButtonActionPerformed
// TODO add your handling code here:
        String res = "String " + stringNombreTextField.getText();
        if (!stringValorTextField.getText().equals("")) {
            res += " = \"" + stringValorTextField.getText() + "\"";
        }
        codeTextArea.setText(res + ";\n" + codeTextArea.getText());
        stringNombreTextField.setText("text" + (++contVar));
        codeTextArea.setCaretPosition(1);
    }//GEN-LAST:event_stringOkButtonActionPerformed

    private void numOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numOkButtonActionPerformed
// TODO add your handling code here:
        String res = "Number " + numTextField.getText();
        res += " = " + ((Integer) numSpinner.getValue()).intValue();
        codeTextArea.setText(res + ";\n" + codeTextArea.getText());
        numTextField.setText("number" + (++contVar));
        codeTextArea.setCaretPosition(1);
    }//GEN-LAST:event_numOkButtonActionPerformed

    private void teclaTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_teclaTextFieldKeyReleased
// TODO add your handling code here:
        teclaTextField.setText(KeyEvent.getKeyText(evt.getKeyCode()));
    }//GEN-LAST:event_teclaTextFieldKeyReleased

    private void teclaTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_teclaTextFieldKeyPressed
// TODO add your handling code here:
        teclaTextField.setText(KeyEvent.getKeyText(evt.getKeyCode()));
    }//GEN-LAST:event_teclaTextFieldKeyPressed

    private void keyOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyOkButtonActionPerformed
// TODO add your handling code here:
        boolean sufijo = false;
        String res = "Macrokey " + keyNombreMyJTextField.getText();
        if (!teclaTextField.getText().equals("") || (altCheckBox.isSelected()) || (ctrlCheckBox.isSelected()) || (shiftCheckBox.isSelected())) {
            res += " = ";
        }
        if (!teclaTextField.getText().equals("")) {
            res += " '" + teclaTextField.getText() + "' ";
            sufijo = true;
        }
        if (altCheckBox.isSelected()) {
            if (sufijo) {
                res += "+";
            }
            res += " '" + KeyEvent.getKeyText(KeyEvent.VK_ALT) + "' ";
            sufijo = true;
        }
        if (ctrlCheckBox.isSelected()) {
            if (sufijo) {
                res += "+";
            }
            res += " '" + KeyEvent.getKeyText(KeyEvent.VK_CONTROL) + "' ";
            sufijo = true;
        }
        if (shiftCheckBox.isSelected()) {
            if (sufijo) {
                res += "+";
            }
            res += " '" + KeyEvent.getKeyText(KeyEvent.VK_SHIFT) + "' ";
        }

        codeTextArea.setText(res + ";\n" + codeTextArea.getText());
        keyNombreMyJTextField.setText("key" + (++contVar));
        codeTextArea.setCaretPosition(1);
    }//GEN-LAST:event_keyOkButtonActionPerformed

    private void colDsdLocCaptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colDsdLocCaptButtonActionPerformed
// TODO add your handling code here:
        if (locXJTextFieldInteger.getText().equals("") || locYJTextFieldInteger.getText().equals("")) {
            System.err.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("ERROR_COORDINATE_LOCATION"));
            return;
        }
        int x = (new Integer(locXJTextFieldInteger.getText())).intValue();
        int y = (new Integer(locYJTextFieldInteger.getText())).intValue();
        Color t = robot.getPixelColor(x, y);
        colRJTextFieldInteger.setText("" + t.getRed());
        colGJTextFieldInteger.setText("" + t.getGreen());
        colBJTextFieldInteger.setText("" + t.getBlue());
    }//GEN-LAST:event_colDsdLocCaptButtonActionPerformed

    private void locColCaptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locColCaptButtonActionPerformed
// TODO add your handling code here:
        Point p;
        Color t;
        while (!robot.isButtonPressed(MouseEvent.BUTTON1) && robot.isNative());
        p = robot.getMousePos();
        t = robot.getPixelColor((int) p.getX(), (int) p.getY());
        locXJTextFieldInteger.setText("" + (int) p.getX());
        locYJTextFieldInteger.setText("" + (int) p.getY());
        colRJTextFieldInteger.setText("" + t.getRed());
        colGJTextFieldInteger.setText("" + t.getGreen());
        colBJTextFieldInteger.setText("" + t.getBlue());
    }//GEN-LAST:event_locColCaptButtonActionPerformed

    private void colOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colOkButtonActionPerformed
// TODO add your handling code here:
        String res = "Color " + colNombreMyJTextField.getText();
        if (!colRJTextFieldInteger.getText().equals("") && !colGJTextFieldInteger.getText().equals("") && !colBJTextFieldInteger.getText().equals("")) {
            res += " = [" + colRJTextFieldInteger.getText() + ", " + colGJTextFieldInteger.getText() + ", " + colBJTextFieldInteger.getText() + "]";
        }
        codeTextArea.setText(res + ";\n" + codeTextArea.getText());
        colNombreMyJTextField.setText("col" + (++contVar));
        codeTextArea.setCaretPosition(1);
    }//GEN-LAST:event_colOkButtonActionPerformed

    private void colCaptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colCaptButtonActionPerformed
// TODO add your handling code here:
        Point p;
        Color t;
        while (!robot.isButtonPressed(MouseEvent.BUTTON1) && NativeRobot.isNative());
        p = robot.getMousePos();
        t = robot.getPixelColor((int) p.getX(), (int) p.getY());
        colRJTextFieldInteger.setText("" + t.getRed());
        colGJTextFieldInteger.setText("" + t.getGreen());
        colBJTextFieldInteger.setText("" + t.getBlue());
    }//GEN-LAST:event_colCaptButtonActionPerformed

    private void locCaptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locCaptButtonActionPerformed
// TODO add your handling code here:

        Point p;
        if (NativeRobot.isNative()) {
            while (!robot.isButtonPressed(MouseEvent.BUTTON1));
        } else {
            p = robot.getMousePos();
            while (p.x > 10 || p.y > 10) {
                this.posCursorLabel.setText("[" + p.x + ", " + p.y + "]");
                p = robot.getMousePos();
            }
        }
        p = robot.getMousePos();
        locXJTextFieldInteger.setText("" + (int) p.getX());
        locYJTextFieldInteger.setText("" + (int) p.getY());
    }//GEN-LAST:event_locCaptButtonActionPerformed

    private void locOkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locOkButtonActionPerformed
// TODO add your handling code here:
        String res = "Location " + locNombreMyJTextField.getText();
        if (!locXJTextFieldInteger.getText().equals("") && !locYJTextFieldInteger.getText().equals("")) {
            res += " = [" + locXJTextFieldInteger.getText() + ", " + locYJTextFieldInteger.getText() + "]";
        }
        codeTextArea.setText(res + ";\n" + codeTextArea.getText());
        locNombreMyJTextField.setText("loc" + (++contVar));
        codeTextArea.setCaretPosition(1);
    }//GEN-LAST:event_locOkButtonActionPerformed

    private void stopMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopMenuItemActionPerformed
// TODO add your handling code here:
        stop();
    }//GEN-LAST:event_stopMenuItemActionPerformed

    private void callMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_callMenuItemActionPerformed
// TODO add your handling code here:
        File fich = abrir();
        if (fich != null) {
            codeTextArea.setText(codeTextArea.getText() + "\ncall \"" + fich.getAbsolutePath() + "\"\n");
        }
    }//GEN-LAST:event_callMenuItemActionPerformed
    boolean captura = false;

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
// TODO add your handling code here:
        stop();
    }//GEN-LAST:event_stopButtonActionPerformed

    private void nuevoMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoMenuItemActionPerformed
// TODO add your handling code here:
        codeTextArea.setText("");
        if (this.getRootPane().getParent() instanceof MainFrame) {
            ((MainFrame) this.getRootPane().getParent()).setTitle(TITULO);
        }
    }//GEN-LAST:event_nuevoMenuItemActionPerformed

    private void runPauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runPauseButtonActionPerformed
// TODO add your handling code here:
        runPause(codeTextArea.getText());
    }//GEN-LAST:event_runPauseButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
// TODO add your handling code here:
        salvar();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
// TODO add your handling code here:
        File fich = abrir();
        if (fich != null) {
            codeTextArea.setText(leerScript(fich));
        }
    }//GEN-LAST:event_openButtonActionPerformed

    private void runPauseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runPauseMenuItemActionPerformed
// TODO add your handling code here:
        runPause(codeTextArea.getText());
    }//GEN-LAST:event_runPauseMenuItemActionPerformed

    private void salirMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirMenuItemActionPerformed
// TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_salirMenuItemActionPerformed

    private void salvarMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarMenuItemActionPerformed
// TODO add your handling code here:
        salvar();
    }//GEN-LAST:event_salvarMenuItemActionPerformed

    private void abrirMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirMenuItemActionPerformed
// TODO add your handling code here:
        File fich = abrir();
        if (fich != null) {
            codeTextArea.setText(leerScript(fich));
        }
    }//GEN-LAST:event_abrirMenuItemActionPerformed

    private void selectOtherWindowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectOtherWindowButtonActionPerformed
        // TODO add your handling code here:
        if (NativeRobot.isNative()) {
            Object[][] list = robot.getAllWindowDesc();
            String[] winList = new String[list.length];
            for (int i = 0; i < list.length; i++) {
                winList[i] = (String) list[i][1];
            }
            WindowsDialog dialog = new WindowsDialog(mainFrame, true, winList);
            dialog.setVisible(true);
            int index = dialog.getSelectedIndex();
            if (index >= 0) {
                robot.setActWinHandle(((Integer) list[index][0]).intValue());

                nombreUoComboBox.setEnabled(false);
                nativeCheckBox.setEnabled(true);
                NativeRobot.setNativeMode(nativeCheckBox.isSelected());
                System.out.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_WINDOW") + NativeRobot.getActWinTittle() + java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_FIND"));

            }
//            else {
//                System.out.println("No se ha vinculado a ninguna ventana");
//            }
        }
    }//GEN-LAST:event_selectOtherWindowButtonActionPerformed

    private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
        // TODO add your handling code here:
        if (aboutDialog == null) {
            aboutDialog = new AboutDialog(mainFrame, true);
        }
        aboutDialog.setVisible(true);
    }//GEN-LAST:event_aboutMenuItemActionPerformed

    private String leerScript(File fich) {
        String codeText = "";
        try {
            LineNumberReader in = new LineNumberReader(new FileReader(fich));
            String code = in.readLine();
            codeText = code;
            code = in.readLine();
            while (code != null) {
                codeText += "\n" + code;
                code = in.readLine();
            }
            in.close();
            // Le ponemos en el titulo el nombre del fichero guardado
            if (this.getRootPane().getParent() instanceof MainFrame) {
                ((MainFrame) this.getRootPane().getParent()).setTitle(TITULO + ": " + fich.getName());
            }
            return codeText;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_ERROR") + e.getClass().getName() + " " + e.getMessage(),
                    java.util.ResourceBundle.getBundle("Sindarin/config").getString("ERROR_READ") + fich, JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }

    private void guardarScript(File fich, String codeText) {
        try {
            FileOutputStream out = new FileOutputStream(fich);
            String code = codeText;
            for (int i = 0; i < code.length(); i++) {
                out.write(code.charAt(i));
            }
            out.close();
            // Le ponemos en el titulo el nombre del fichero guardado
            if (this.getRootPane().getParent() instanceof MainFrame) {
                ((MainFrame) this.getRootPane().getParent()).setTitle(TITULO + ": " + fich.getName());
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Sindarin/config").getString("TEXT_ERROR") + e.getClass().getName() + " " + e.getMessage(),
                    java.util.ResourceBundle.getBundle("Sindarin/config").getString("ERROR_WRITE") + fich, JOptionPane.ERROR_MESSAGE);
        }
    }

    private File abrir() {
        int returnVal = chooser.showOpenDialog(getParent());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        }
        return null;
    }

    private void salvar() {
        int returnVal = chooser.showSaveDialog(getParent());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            String tmpStr = file.toString();
            if (tmpStr.substring(tmpStr.length() - 4).toLowerCase().charAt(0) != '.') {
                file = new File(file.getAbsoluteFile() + EXT_SCR);
            }
            guardarScript(file, codeTextArea.getText());
        }
    }

    private void run(String codeText) {
        StringReader in = new StringReader(codeText);
        ScriptParser parser = new ScriptParser(in);

        consolaJOutputArea.setText("");                             // Limpia la consola

        parser.inic();
        try {
            parser.begin();
        } catch (Exception e) {
            System.out.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("ERROR_SINTAX"));
            e.printStackTrace();
            return;
        }

        try {
            Vector inst = parser.getInstVector();
            if (debug) {
                for (int i = 0; i < inst.size(); i++) {
                    System.out.println("PC: " + i + " " + ((Inst) inst.elementAt(i)));
                }
            }

            vm = new CodeInterpreter(inst, parser.getSimbolTable(), robot);
            vm.start();
            if (tecla.isStop()) {
                tecla = new AtrapaTecla(this, robot, KeyEvent.VK_PAUSE);
                tecla.start();
            }
            stopButton.setEnabled(true);
            stopMenuItem.setEnabled(true);
            runPauseButton.setEnabled(false);
            runPauseMenuItem.setEnabled(false);
        } catch (Exception e) {
            System.out.println(java.util.ResourceBundle.getBundle("Sindarin/config").getString("ERROR_RUNNING"));
            e.printStackTrace();
        }
    }

    private void runPause(String codeText) {
        if ((vm == null) || (!vm.isPaused())) {
            run(codeText);
        }
    }

    public void stop() {
        vm.stop();
        tecla.stop();
//        vm = null;
        System.gc();
        stopButton.setEnabled(false);
        stopMenuItem.setEnabled(false);
        runPauseButton.setEnabled(true);
        runPauseMenuItem.setEnabled(true);
    }

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CtrlVarPanel;
    private javax.swing.JTabbedPane VarTabbedPane;
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem abrirMenuItem;
    private javax.swing.JCheckBox altCheckBox;
    private javax.swing.JMenu archivoMenu;
    private javax.swing.JMenuItem beepMenuItem;
    private javax.swing.JMenuItem breakMenuItem;
    private javax.swing.JMenuItem callMenuItem;
    private javax.swing.JMenuItem clickMenuItem;
    private javax.swing.JButton colCaptButton;
    private javax.swing.JButton colDsdLocCaptButton;
    private javax.swing.JTextField colNombreMyJTextField;
    private javax.swing.JButton colOkButton;
    private javax.swing.JMenuItem colorMenuItem;
    private javax.swing.JPanel colorPanel;
    private javax.swing.JMenuItem componentMenuItem;
    private javax.swing.JMenuItem condIfMenuItem;
    private javax.swing.JCheckBox ctrlCheckBox;
    private javax.swing.JPanel ctrlPanel;
    private javax.swing.JMenuItem dateMenuItem;
    private javax.swing.JMenuItem dclickMenuItem;
    private javax.swing.JMenuItem dragMenuItem;
    private javax.swing.JPopupMenu editPopupMenu;
    private javax.swing.JMenu ejecMenu;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenuItem falseMenuItem;
    private javax.swing.JButton findUoButton;
    private javax.swing.JMenu instMenu;
    private javax.swing.JMenuItem iodelayMenuItem;
    private javax.swing.JMenuItem isnativemodeMenuItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JTextField keyNombreMyJTextField;
    private javax.swing.JButton keyOkButton;
    private javax.swing.JPanel keyPanel;
    private javax.swing.JMenuItem kpressMenuItem;
    private javax.swing.JMenuItem kreleaseMenuItem;
    private javax.swing.JButton locCaptButton;
    private javax.swing.JButton locColCaptButton;
    private javax.swing.JTextField locNombreMyJTextField;
    private javax.swing.JButton locOkButton;
    private javax.swing.JPanel locPanel;
    public javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mousemoveMenuItem;
    private javax.swing.JMenuItem mousepressMenuItem;
    private javax.swing.JMenuItem mousereleaseMenuItem;
    private javax.swing.JCheckBox nativeCheckBox;
    private javax.swing.JComboBox nombreUoComboBox;
    private javax.swing.JMenuItem nuevoMenuItem;
    private javax.swing.JButton numOkButton;
    private javax.swing.JSpinner numSpinner;
    private javax.swing.JTextField numTextField;
    private javax.swing.JList numberList;
    private javax.swing.JPanel numberPanel;
    private javax.swing.JButton openButton;
    private javax.swing.JMenuItem pkeyMenuItem;
    private javax.swing.JLabel posCursorLabel;
    private javax.swing.JMenuItem printMenuItem;
    private javax.swing.JMenuItem repeatMenuItem;
    private javax.swing.JButton runPauseButton;
    private javax.swing.JMenuItem runPauseMenuItem;
    private javax.swing.JMenuItem salirMenuItem;
    private javax.swing.JMenuItem salvarMenuItem;
    private javax.swing.JButton saveButton;
    private javax.swing.JMenuItem sayMenuItem;
    private javax.swing.JButton selectOtherWindowButton;
    private javax.swing.JCheckBox shiftCheckBox;
    private javax.swing.JMenuItem sindarin_revisionMenuItem;
    private javax.swing.JMenuItem sindarin_versionMenuItem;
    private javax.swing.JButton stopButton;
    private javax.swing.JMenuItem stopMenuItem;
    private javax.swing.JTextField stringNombreTextField;
    private javax.swing.JButton stringOkButton;
    private javax.swing.JPanel stringPanel;
    private javax.swing.JTextField stringValorTextField;
    private javax.swing.JTextField teclaTextField;
    private javax.swing.JMenuItem timeMenuItem;
    private javax.swing.JMenuItem trueMenuItem;
    private javax.swing.JMenuItem waitMenuItem;
    private javax.swing.JMenuItem whileMenuItem;
    // End of variables declaration//GEN-END:variables

    /**
     * @param mainFrame the mainFrame to set
     */
    public void setMainFrame(MainFrame mainFrame) {
        if (mainFrame != null) {
            this.mainFrame = mainFrame;
            aboutMenuItem.setVisible(true);
            jSeparator2.setVisible(true);
            mainFrame.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/Sindarin/Imagen/s.png")).getImage());
        }
    }
}
